# coding=utf-8
import time

import traceback
from monthdelta import monthdelta
import datetime
import requests
from pymongo import MongoClient
import Utils
import sys
from pandas import DataFrame
import pandas as pd
import json

reload(sys)
sys.setdefaultencoding('utf-8')


mgClient = MongoClient('121.40.54.235', 27017,
    username='root2',
    password='Dream2015',
    authSource="stockDataStore",
    authMechanism='SCRAM-SHA-1')
db = mgClient['stockDataStore']


not_number_item_names = ['流动资产', '非流动资产', '流动负债', '非流动负债', '所有者权益', '经营活动产生的现金流量', '投资活动产生的现金流量', '筹资活动产生的现金流量', '附注'];
nnot_number_item_names_of_finance_stock = ['资产', '负债', '每股收益']

categories = {};
categories['balance_sheet'] = ['资产', '负债', '流动资产', '非流动资产', '资产总计', '流动负债', '非流动负债', '所有者权益'];
categories['profit_statement'] = ['营业收入', '营业支出', '营业总收入', '营业总成本', '营业利润','利润总额','净利润', '每股收益', '其他综合收益', '综合收益总额'];
categories['cash_flow'] = ['经营活动产生的现金流量', '投资活动产生的现金流量' ,'筹资活动产生的现金流量','汇率变动对现金及现金等价物的影响','现金及现金等价物净增加额','期末现金及现金等价物余额', '附注' ];


def get_profile(stock_code, type):
    one = db[type+"_profile"].find_one({"code": stock_code})
    item_names = one["item_names"]
    return item_names

def gen_indices_list(stock_code, type):
    item_names = get_profile(stock_code, type)
    indices_list = []
    for item_name in item_names:
        #if item_name == '报表日期':
            #continue
        category_name_array = categories[type]
        is_category = 0
        if item_name in category_name_array:
            is_category = 1
        is_number = 1
        if item_name in not_number_item_names or item_name in nnot_number_item_names_of_finance_stock:
            is_number = 0
        indices_list.append({"label": item_name, "value": item_name, 'category': is_category, 'number': is_number})
    return indices_list


finance_indices_type = {}


finance_indices_type['balance_sheet'] = gen_indices_list('600009', 'balance_sheet')
finance_indices_type['balance_sheet_of_bank'] = gen_indices_list('600036', 'balance_sheet')
finance_indices_type['balance_sheet_of_security'] = gen_indices_list('600030', 'balance_sheet')
finance_indices_type['balance_sheet_of_insurance'] = gen_indices_list('601318', 'balance_sheet')

finance_indices_type['profit_statement'] = gen_indices_list('600009', 'profit_statement')
finance_indices_type['profit_statement_of_bank'] = gen_indices_list('600036', 'profit_statement')
finance_indices_type['profit_statement_of_security'] = gen_indices_list('600030', 'profit_statement')
finance_indices_type['profit_statement_of_insurance'] = gen_indices_list('601318', 'profit_statement')

finance_indices_type['cash_flow'] = gen_indices_list('600009', 'cash_flow')
finance_indices_type['cash_flow_of_bank'] = gen_indices_list('600036', 'cash_flow')
finance_indices_type['cash_flow_of_security'] = gen_indices_list('600030', 'cash_flow')
finance_indices_type['cash_flow_of_insurance'] = gen_indices_list('601318', 'cash_flow')

result = json.dumps(finance_indices_type, ensure_ascii=False, indent=4, sort_keys=True)
Utils.save_to_new_file("category.json", result)
print result